<?php

/**
 * 后台用户控制器
 *
 * @author Fong.zhang
 */
namespace Home\Controller;

class UserController extends BaseController
{
    public function listAction() {
        $status = I('get.status') === '' ? '1' : '0';

        $admin_model = D('Admin');

        $condition = array(
            'status' => $status,
            'is_deleted' => '0'
        );

        $adminList = $admin_model->where($condition)->select();

        $this->assign(array(
            'adminList' => $adminList
        ));

        $this->display();
    }

    public function addAction () {
        $error = [];

        if (IS_POST) {
            $role = I('post.role');
            $code = I('post.code');
            $name = I('post.name');
            $email = I('post.email');
            $password = I('post.password');
            $repeat_password = I('post.repeat_password');
            $qq = I('post.qq');
            $sex = I('post.sex');
            $mobile = I('post.mobile');

            if ($password !== $repeat_password) {
                $error['repeat_password'] = '密码不一致！';
            }

            if (!$code) {
                $error['code'] = '编号不能为空！';
            }

            if (!$name) {
                $error['name'] = '姓名不能为空！';
            }

            if (!$email) {
                $error['email'] = '邮箱不能为空！';
            }

            if (!$mobile) {
                $error['mobile'] = '电话不能为空！';
            }

            if (!$error) {
                $server_pool = array(
                    'http://123.57.22.77/headuser/add'
                );

                foreach ($server_pool as $server) {
                    $post_data = array (
                        'role' => $role,
                        "code" => $code,
                        "name" => $name,
                        "email" => $email,
                        "password" => md5($password),
                        "qq" => $qq,
                        "sex" => $sex,
                        "mobile" => $mobile,
                        'sso' => '1',
                        'sessionKey' => session('sso_session_key')
                    );

                    $ch = curl_init();

                    curl_setopt($ch, CURLOPT_URL, $server);
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                    // post数据
                    curl_setopt($ch, CURLOPT_POST, 1);
                    // post的变量
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

                    $output = curl_exec($ch);
                    curl_close($ch);
                }

                $admin_model = D('admin');

                $admin = array(
                    'code' => $code,
                    'name' => $name,
                    'sex' => $sex,
                    'entry_time' => time(),
                    'mobile' => $mobile,
                    'qq' => $qq,
                    'email' => $email,
                    'password' => md5($password),
                    'role' => $role,
                );

                $admin_model->add($admin);

                $this->ajaxReturn(array(
                    'error' => $error
                ));
            }
        }

        $this->display();
    }

    public function editAction () {
        $error = [];
        $id = I('get.id') ? I('get.id') : I('post.id');

        $admin_model = D('Admin');

        $condition = array(
            'id' => $id,
            'is_deleted' => '0'
        );

        $admin = $admin_model->where($condition)->find();

        if (IS_POST) {
            $role = I('post.role');
            $code = I('post.code');
            $name = I('post.name');
            $email = I('post.email');
            $password = I('post.password');
            $repeat_password = I('post.repeat_password');
            $qq = I('post.qq');
            $sex = I('post.sex');
            $mobile = I('post.mobile');

            if ($password !== $repeat_password) {
                $error['repeat_password'] = '密码不一致！';
            }

            if (!$code) {
                $error['code'] = '编号不能为空！';
            }

            if (!$name) {
                $error['name'] = '姓名不能为空！';
            }

            if (!$email) {
                $error['email'] = '邮箱不能为空！';
            }

            if (!$mobile) {
                $error['mobile'] = '电话不能为空！';
            }

            if (mb_strlen($mobile) != '11') {
                $error['mobile'] = '电话格式错误！';
            }

            if (!$error) {
                $server_pool = array(
                    'http://123.57.22.77'
                );

                foreach ($server_pool as $server) {
                    $user_data = array(
                        'email' => $admin['email'],
                        'sso' => '1',
                        'sessionKey' => session('sso_session_key')
                    );

                    $ch = curl_init();

                    curl_setopt($ch, CURLOPT_URL, $server.'/headuser/getUserId');
                    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                    // post数据
                    curl_setopt($ch, CURLOPT_POST, 1);
                    // post的变量
                    curl_setopt($ch, CURLOPT_POSTFIELDS, $user_data);

                    $output = curl_exec($ch);
                    curl_close($ch);

                    $user_call_back = json_decode($output, true);

                    if ($user_call_back) {
                        $post_data = array (
                            'id' => $user_call_back['id'],
                            'role' => $role,
                            "code" => $code,
                            "name" => $name,
                            "email" => $email,
                            "qq" => $qq,
                            "sex" => $sex,
                            "mobile" => $mobile,
                            'sso' => '1',
                            'sessionKey' => session('sso_session_key')
                        );

                        if ($password) $post_data['password'] = md5($password);

                        $ch = curl_init();

                        curl_setopt($ch, CURLOPT_URL, $server.'/headuser/edit');
                        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                        // post数据
                        curl_setopt($ch, CURLOPT_POST, 1);
                        // post的变量
                        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);

                        $output = curl_exec($ch);
                        curl_close($ch);
                    }
                }

                $admin_model = D('admin');

                $admin = array(
                    'id' => $id,
                    'code' => $code,
                    'name' => $name,
                    'sex' => $sex,
                    'entry_time' => time(),
                    'mobile' => $mobile,
                    'qq' => $qq,
                    'email' => $email,
                    'role' => $role,
                );

                if ($password) $admin['password'] = md5($password);

                $admin_model->save($admin);

                $this->ajaxReturn(array(
                    'error' => $error
                ));
            }
        }

        $this->assign(array(
            'admin' => $admin
        ));

        $this->display();
    }
}